package debtbundler.domain.entity.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import debtbundler.domain.entity.BalanceBook;
import debtbundler.domain.entity.BalanceSheet;

class BalanceBookImpl extends AbstractListenableEntity implements BalanceBook {
	private static final long serialVersionUID = 1L;
	private static Logger logger = Logger.getLogger(BalanceBookImpl.class.getName());
	
	protected List<BalanceSheet> balanceSheets = new ArrayList<BalanceSheet>();
	
	protected BalanceBookImpl() {
		super();
		logger.log(Level.FINE, "Created balance book");
	}

	@Override
	public boolean addBalanceSheet(BalanceSheet sheet) {
		if (!balanceSheets.contains(sheet)) {
			boolean added = balanceSheets.add(sheet);
			if (added) {
				fireEntityUpdateEvent();
				logger.log(Level.FINE, "Added balance sheet: {0}", sheet);
			} else {
				logger.log(Level.FINE, "Already contains balance sheet: {0}", sheet);
			}
			return added;
		} else {
			return false;
		}
	}

	@Override
	public List<BalanceSheet> getBalanceSheets() {
		return Collections.unmodifiableList(balanceSheets);
	}

	@Override
	public boolean removeBalanceSheet(BalanceSheet sheet) {
		boolean removed = balanceSheets.remove(sheet);
		if (removed) {
			fireEntityUpdateEvent();
			logger.log(Level.FINE, "Removed balance sheet: {0}", sheet);
		} else {
			logger.log(Level.FINE, "Does not contain sheet: {0}", sheet);
		}
		return removed;
	}

	@Override
	public String toString() {
		return "Balance book created " + getCreatedDate().getTime();
	}
}
